<?php

/**
 * directories actions.
 *
 * @package    master_directory
 * @subpackage directories
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class directoriesActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  	$this->form = new MasterDirectoryForm();
  	
   // $this->master_directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
   
  	$master_directories = array();
  	
  	$all_directories =  $this->master_directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
  	
  	array_push($master_directories, Doctrine_Core::getTable('MasterDirectory')->find(array($this->getUser()->getAttribute("directory_id"))));
   
  	$this->master_directories = $this->getUser()->getAttribute('rol_id') == sfConfig::get('app_admin') ? $all_directories : $master_directories; 
    
    $this->directories_types = Doctrine_Core::getTable('DirectoryType')->retreiveActiveTypes(); 
    
    $this->languages = Doctrine_Core::getTable('Language')->getAll(); 
    
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->master_directory = Doctrine_Core::getTable('MasterDirectory')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->master_directory);
    
    $this->form = new MasterDirectoryForm($this->master_directory);
    
    $all_directories =  $this->master_directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
    
    $directories_types = Doctrine_Core::getTable('DirectoryType')->retreiveActiveTypes(); 
    
    $master_directories = array();
    array_push($master_directories, Doctrine_Core::getTable('MasterDirectory')->find(array($this->getUser()->getAttribute("directory_id"))));
    
    $this->master_directories = $this->getUser()->getAttribute('rol_id') == sfConfig::get('app_admin') ? $all_directories : $master_directories;
    
    $languages = Doctrine_Core::getTable('Language')->getAll(); 
    
    return $this->renderPartial('directory',array('form' => $this->form, 'directories_types' => $directories_types, 'master_directories' => $this->master_directories, 'languages' => $languages));
    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new MasterDirectoryForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    

    $form = new MasterDirectoryForm();

    $this->processForm($request, $form);

    //$this->setTemplate('new');
    
    //$this->master_directories = Doctrine_Core::getTable('MasterDirectory')->createQuery('a')->execute();
    
    $this->master_directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
    
    $this->form = new MasterDirectoryForm();
    
    $directories_types = Doctrine_Core::getTable('DirectoryType')->retreiveActiveTypes(); 
    
    $languages = Doctrine_Core::getTable('Language')->getAll(); 
    
	$action_str = "Directory created ID: " . $form->getObject()->getId();
  	DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));    
    
    return $this->renderPartial('directory_container',array('master_directories'=>$this->master_directories, 'form' => $this->form, 'directories_types' => $directories_types, 'languages' => $languages));
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($master_directory = Doctrine_Core::getTable('MasterDirectory')->find(array($request->getParameter('id'))), sprintf('Object master_directory does not exist (%s).', $request->getParameter('id')));
    $this->form = new MasterDirectoryForm($master_directory);
    
    
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($master_directory = Doctrine_Core::getTable('MasterDirectory')->find(array($request->getParameter('id'))), sprintf('Object master_directory does not exist (%s).', $request->getParameter('id')));
    $form = new MasterDirectoryForm($master_directory);

    $this->processForm($request, $form);
    
    $master_directories = array();
    
    array_push($master_directories, Doctrine_Core::getTable('MasterDirectory')->find(array($this->getUser()->getAttribute("directory_id"))));
    
    $all_directories =  $this->master_directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
    $this->master_directories = $this->getUser()->getAttribute('rol_id') == sfConfig::get('app_admin') ? $all_directories : $master_directories;

    
    $this->form = new MasterDirectoryForm();
    
    $directories_types = Doctrine_Core::getTable('DirectoryType')->retreiveActiveTypes(); 
    
    $languages = Doctrine_Core::getTable('Language')->getAll(); 
    
    $action_str = "Directory updated ID: " . $form->getObject()->getId();
  	DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));  
    
    return $this->renderPartial('directory_container',array('master_directories'=>$this->master_directories, 'form' => $this->form, 'directories_types' => $directories_types, 'languages' => $languages));
    
  }

  public function executeDelete(sfWebRequest $request)
  {
    //$request->checkCSRFProtection();

    $this->forward404Unless($master_directory = Doctrine_Core::getTable('MasterDirectory')->find(array($request->getParameter('id'))), sprintf('Object master_directory does not exist (%s).', $request->getParameter('id')));
    $master_directory->setDeleted(1);
    $master_directory->save();
    
    $this->getUser()->setFlash('save', 'deleted', false);
    
    $this->master_directories = Doctrine_Core::getTable('MasterDirectory')->retreiveActiveDirectories();
    
    $this->form = new MasterDirectoryForm();
    
    $directories_types = Doctrine_Core::getTable('DirectoryType')->retreiveActiveTypes();

    $languages = Doctrine_Core::getTable('Language')->getAll(); 
    
    $action_str = "Directory deleted ID: " . $master_directory->getId();
  	DirectoryUtils::registerTrace($action_str, $this->getUser()->getAttribute("user_id"));      
    
    return $this->renderPartial('directory_container',array('master_directories'=>$this->master_directories, 'form' => $this->form, 'directories_types' => $directories_types, 'languages' => $languages));
    
    
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
   /* if ($form->isValid())
    {*/
      $master_directory = $form->save();
      
      $this->getUser()->setFlash('save', 'saved', false);

      //$this->redirect('directories/edit?id='.$master_directory->getId());
   // }
  }
  
}
